/// 
/// MaDAson - Toolkit for Model Driven Software Development
/// 
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Core;
using NUnit.Framework;
using Madason.MetaModel.Interfaces;
using System.IO;

namespace Madason.MetaModel.Interfaces.Tests
{
	[TestFixture]
	public class TestImportXmi11Namespaces : TestImportXmi11Base
	{
		string simpleXmi =
			@"<XMI xmi.version=""1.1"" xmlns:UML=""href://org.omg/UML/1.3"" timestamp=""Thu Nov 23 19:17:58 2006"">" +
			@"	<XMI.content>" +
			@"		<UML:Model xmi.id=""UMLProject.1"">" +
			@"			<UML:Namespace.ownedElement>" +
			@"				<UML:Package xmi.id=""UMLPackage.2"" name=""FirstNamespace"" visibility=""public"" isSpecification=""false"" namespace=""UMLProject.1"" isRoot=""false"" isLeaf=""false"" isAbstract=""false"">" +
			@"					<UML:Namespace.ownedElement>" +
			@"						<UML:Package xmi.id=""UMLPackage.2"" name=""ChildNamespace"" visibility=""public"" isSpecification=""false"" namespace=""UMLProject.1"" isRoot=""false"" isLeaf=""false"" isAbstract=""false"">" +
			@"						</UML:Package>" +
			@"					</UML:Namespace.ownedElement>" +
			@"				</UML:Package>" +
			@"			</UML:Namespace.ownedElement>" +
			@"		</UML:Model>" +
			@"	</XMI.content>" +
			@"</XMI>";
		

		/// <summary>
		/// Test loading a simple Model, we assume a model is loaded
		/// </summary>
		[Test]
		public void TestLoadSimplesModel()
		{			
			ImportXmi11 import = new ImportXmi11();
			StringReader reader = new StringReader(simpleXmi);
			Model model = import.Import(reader);
			Assert.IsNotNull(model);
		}

		/// <summary>
		/// Test loading a simple Model with a root-Namespace
		/// </summary>
		[Test]
		public void TestLoadRootNamespace()
		{
			ImportXmi11 import = new ImportXmi11();
			StringReader reader = new StringReader(simpleXmi);
			Model model = import.Import(reader);
			Assert.IsNotNull(model);
			Assert.AreEqual(model.Namespaces.Count, 1);

			Namespace rootNamespace = model.Namespaces[0] as Namespace;
			Assert.AreEqual("FirstNamespace", rootNamespace.Name);
		}

		/// <summary>
		/// Test loading a simple Model with a root-namespace and one child-namespace
		/// </summary>
		[Test]
		public void TestLoadChildNamespace()
		{
			ImportXmi11 import = new ImportXmi11();
			StringReader reader = new StringReader(simpleXmi);
			Model model = import.Import(reader);
			Assert.IsNotNull(model);
			Assert.AreEqual(model.Namespaces.Count, 1);
			Namespace rootNamespace = model.Namespaces[0] as Namespace;

			Assert.AreEqual(rootNamespace.Namespaces.Count, 1);
			Namespace childNamespace = rootNamespace.Namespaces[0] as Namespace;

			Assert.AreEqual("ChildNamespace", childNamespace.Name);
		}


		[Test]
		public void TestEmptyNamespaceElement()
		{
			string emptyNamespaceXmi =
				@"<XMI xmi.version=""1.1"" xmlns:UML=""href://org.omg/UML/1.3"" timestamp=""Thu Nov 23 19:17:58 2006"">" +
				@"	<XMI.content>" +
				@"		<UML:Model xmi.id=""UMLProject.1"">" +
				@"			<UML:Namespace.ownedElement>" +
				@"				<UML:Package xmi.id=""UMLPackage.3"" name=""EmptyNamespace"" visibility=""public"" isSpecification=""false"" namespace=""UMLProject.1"" isRoot=""false"" isLeaf=""false"" isAbstract=""false""/>" +
				@"				<UML:Package xmi.id=""UMLPackage.2"" name=""SecondNamespace"" visibility=""public"" isSpecification=""false"" namespace=""UMLProject.1"" isRoot=""false"" isLeaf=""false"" isAbstract=""false"">" +
				@"					<UML:Namespace.ownedElement>" +
				@"						<UML:Package xmi.id=""UMLPackage.2"" name=""ChildNamespace"" visibility=""public"" isSpecification=""false"" namespace=""UMLProject.1"" isRoot=""false"" isLeaf=""false"" isAbstract=""false"">" +
				@"						</UML:Package>" +
				@"					</UML:Namespace.ownedElement>" +
				@"				</UML:Package>" +
				@"			</UML:Namespace.ownedElement>" +
				@"		</UML:Model>" +
				@"	</XMI.content>" +
				@"</XMI>";


			Model model = LoadModel(emptyNamespaceXmi);
			Assert.AreEqual(2, model.Namespaces.Count);
			Assert.AreEqual("SecondNamespace", (model.Namespaces[1] as Namespace).Name);
		}

		[Test]
		public void TestStereotypeAfterwards()
		{
			string emptyNamespaceXmi =
				@"<XMI xmi.version=""1.1"" xmlns:UML=""href://org.omg/UML/1.3"" timestamp=""Thu Nov 23 19:17:58 2006"">" +
				@"	<XMI.content>" +
				@"		<UML:Model xmi.id=""UMLProject.1"">" +
				@"			<UML:Namespace.ownedElement>" +
				@"				<UML:Package name=""set"" xmi.id=""EAPK_36049459_EF30_4fa4_BC56_794A1A35B1A9"" isRoot=""false"" isLeaf=""false"" isAbstract=""false"" visibility=""public"">"+
				@"					<UML:ModelElement.stereotype>"+
				@"						<UML:Stereotype xmi.idref=""EAID_19B7C23D_3BB3_4a00_AAE1_5E69A918E05B""/>"+
				@"					</UML:ModelElement.stereotype>"+
				@"					<UML:Stereotype xmi.id=""EAID_19B7C23D_3BB3_4a00_AAE1_5E69A918E05B"" name=""MavenMaster"" isRoot=""false"" isLeaf=""false"" isAbstract=""false"">"+
				@"						<UML:Stereotype.baseClass>Package</UML:Stereotype.baseClass>"+
				@"					</UML:Stereotype>"+
				@"				</UML:Package>"+
				@"			</UML:Namespace.ownedElement>" +
				@"		</UML:Model>" +
				@"	</XMI.content>" +
				@"</XMI>";

			ImportXmi11 import = new ImportXmi11();
			StringReader reader = new StringReader(emptyNamespaceXmi);
			Model model = import.Import(reader);
			Assert.IsNotNull(model);

			Namespace ns = model.Namespaces[0];
			Assert.AreEqual("MavenMaster", ns.StereotypeName);
		}
	}
}

